<?php
	if(count($_POST)>0)
	{
		// [idLoai] => 1 [TenSP] => [Gia] => [MoTa] => [ChiTiet] => [TonKho] => [GhiChu] => [AnHien] => 1
		$idCategory=$_POST['idCategory'];
		$name=$_POST['name'];
                if(!empty($_POST['description'])) $description=$_POST['description'];
		else $description='';
		$link=$_POST['link'];
		$AnHien=$_POST['AnHien'];
		
		$file=$_FILES['UrlHinh'];
		//Xu ly upload file
		if($file['name']!='')//Neu co file
		{
			//Lay ten cua file de dua vo csdl
			$UrlHinh=$file['name'];
			//Copy file tu thu muc temp -> thu muc chua file
			copy($file['tmp_name'],"img/app/".$UrlHinh);
		}
		//insert du lieu vao csdl
		
		$sql="INSERT INTO `free-app-store`.`app` (`id`, `name`, `description`, `image`, `rating`, `link`, `downloaded`, `show_hide`) VALUES (NULL, '$name', '$description', '$UrlHinh', 0, '$link', 0, '1')";
                mysqli_query($conn, $sql);
                $sql="Select * from app order by id desc";
                $rs = mysqli_query($conn, $sql);
                $r = mysqli_fetch_assoc($rs);
                $sql="INSERT INTO category_app values('$idCategory','".$r['id']."')";
                mysqli_query($conn, $sql);
		header('location:?mod=app&idCategory='.$idCategory);
		//print_r($UrlHinh);
		//echo $UrlHinh['name'],'<br/>';
		//echo $UrlHinh['type'],'<br/>';
		//echo $UrlHinh['tmp_name'],'<br/>';
		//echo $UrlHinh['error'],'<br/>';
		//echo $UrlHinh['size'];
		
		
	}
?>
<form action="" method="post" enctype="multipart/form-data" name="form1">
  <table width="400" border="1" style="width:400px;margin:auto">
    <caption>
      THÊM SẢN PHẨM
    </caption>
      <a href ="?mod=app">Back to APP</a>
    <tr>
      <th width="155" scope="row">Category</th>
      <td width="229"><select name="idCategory" id="idCategory">
        <?php
		//$idLoai=round($_GET['idLoai']);
		$sql="SELECT * FROM category";
		$rs=mysqli_query($conn,$sql);
		while($r=mysqli_fetch_assoc($rs))
		{
	?>
        <option <?php if($r['id']==$_GET['idCategory']) echo 'selected' ?> value="<?php echo $r['id']?>"><?php echo $r['name']?></option>
        <?php	
        }
	?>
      </select></td>
    </tr>
    <tr>
      <th width="155" scope="row">Name</th>
      <td><label>
        <input name="name" type="text" id="name" size="40">
      </label></td>
    </tr>
    <tr>
      <th scope="row">Description</th>
      <td><label>
        <textarea name="desciption" id="desciption" cols="30" rows="5"></textarea>
      </label></td>
    </tr>
    <tr>
      <th scope="row">Image</th>
      <td><label>
        <input type="file" name="UrlHinh" id="UrlHinh">
      </label></td>
    </tr>
    <tr>
      <th scope="row">Link</th>
      <td><label>
        <input type="text" name="link" id="link">
      </label></td>
    </tr>
    <tr>
      <th scope="row">Show/Hide</th>
      <td><p>
        <label>
          <input name="AnHien" type="radio" id="AnHien_0" value="1" checked>
          Hiện</label>
        <br>
        <label>
          <input type="radio" name="AnHien" value="0" id="AnHien_1">
          Ẩn</label>
        <br>
      </p></td>
    </tr>
    <tr>
      <th scope="row">&nbsp;</th>
      <td><label>
        <input type="reset" name="reset" id="reset" value="Nhập lại">
        <input type="submit" name="submit" id="submit" value="Thêm APP">
      </label></td>
    </tr>
  </table>
</form>
